#
# @lc app=leetcode.cn id=1031 lang=python3
#
# [1031] 两个非重叠子数组的最大和
#

# @lc code=start
from itertools import accumulate
from typing import List

#最大值的情况无非有两种 左a右b 左b右a
class Solution:
    def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) -> int:
        s = list(accumulate(nums,initial = 0))
        ans = 0
        # 假设b的下标范围为[i-secondLen,i)
        def f(firstLen : int, secondLen : int) -> None:
            nonlocal ans
            maxSumA = 0
            for i in range(firstLen+secondLen,len(s)):
                maxSumA = max(maxSumA,s[i-secondLen] - s[i-secondLen-firstLen]) #长度为firstLen的最大和
                ans = max(ans,maxSumA + s[i] - s[i-secondLen])
        f(firstLen,secondLen) #左a右b 
        f(secondLen,firstLen) #左b右a
        return ans
# @lc code=end

